﻿namespace Agile.Infra.Core.Agile;

public class Appsettings
{
    private static IConfiguration _instance;
    public static IConfiguration Configuration
    {
        get
        {
            if (_instance == null)
            {
                throw new Exception("使用前请初始化 Appsettings.Initialization(configuration);");
            }

            return _instance;
        }
    }

    public static string _applicationCode;
    public static string ApplicationCode
    {
        get
        {
            if (string.IsNullOrWhiteSpace(_applicationCode))
            {
                _applicationCode = AppRequired("ApplicationCode");
            }
            return _applicationCode;
        }
    }

    public static void Initialization(IConfiguration configuration) => _instance = configuration;

    /// <summary>
    /// 封装要操作的字符
    /// </summary>
    /// <param name="sections">节点配置</param>
    /// <returns></returns>
    public static string App(params string[] sections)
    {
        try
        {
            if (sections.Any())
            {
                return Configuration[string.Join(":", sections)];
            }
        }
        catch (Exception) { }
        return "";
    }

    /// <summary>
    /// 封装要操作的字符
    /// </summary>
    /// <param name="sections">节点配置</param>
    /// <returns></returns>
    public static T App<T>(params string[] sections)
    {
        var value = App(sections);
        return (T)Convert.ChangeType(value, typeof(T));
    }

    /// <summary>
    /// 封装要操作的字符
    /// </summary>
    /// <param name="sections">节点配置</param>
    /// <returns></returns>
    public static string AppRequired(params string[] sections)
    {
        var value = App(sections);
        if (string.IsNullOrWhiteSpace(value))
        {
            throw new Exception("appsettings配置异常:" + string.Join(":", sections));
        }
        return value;
    }

    /// <summary>
    /// 封装要操作的字符
    /// </summary>
    /// <param name="sections">节点配置</param>
    /// <returns></returns>
    public static T AppRequired<T>(params string[] sections)
    {
        var value = AppRequired(sections);
        return (T)Convert.ChangeType(value, typeof(T));
    }

    /// <summary>
    /// 递归获取配置信息数组
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="sections"></param>
    /// <returns></returns>
    public static List<T> AppList<T>(params string[] sections)
    {
        List<T> list = new List<T>();
        // 引用 Microsoft.Extensions.Configuration.Binder 包
        if (sections.Any())
        {
            Configuration.Bind(string.Join(":", sections), list);
        }
        return list;
    }

    /// <summary>
    /// 递归获取配置信息数组
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="sections"></param>
    /// <returns></returns>
    public static IConfiguration Section(params string[] sections)
    {
        if (sections.Any())
        {
            return Configuration.GetSection(string.Join(":", sections));
        }
        return null;
    }
}
